package com.tuozixuan.onesql.ui;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Lists;
import com.tuozixuan.onesql.service.TableContext;
import com.tuozixuan.onesql.service.TableServiceImpl;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import javafx.scene.layout.HBox;

public class ConditionFilterBox {
    
    private HBox hBox = new HBox();
    
    private int conditionCount;
    
    private List<ConditionFilterBoxItem> conditionFilterBoxItemList = Lists.newArrayList();

    public ConditionFilterBox(TableContext tableContext, TableView table) {
        this.createConditionFilterControl(tableContext);
        
        Button addButton =  new Button();
        addButton.setText("+");
        addButton.setOnAction((ActionEvent event) -> {
        	this.createConditionFilterControl(tableContext);
        });
        hBox.getChildren().add(addButton);
        
        Button queryButton =  new Button();
        queryButton.setText("查询");
        queryButton.setOnAction((ActionEvent event) -> {
            
            String queryCondition = conditionFilterBoxItemList.stream().filter(item->StringUtils.isNotBlank(item.getSqlConditionStr())).map(item -> item.getSqlConditionStr()).collect(Collectors.joining(" and "));
            if (StringUtils.isNotBlank(queryCondition)) {
            	queryCondition = " where " + queryCondition;
            }
            System.out.println("queryCondition: " + queryCondition);
            
            ObservableList<Map<String,Object>> tableData = this.buildTableData(tableContext.getTableName(), queryCondition);
            table.setItems(tableData);
            
        });
        hBox.getChildren().add(queryButton);
        hBox.autosize();
    }
    
    private void createConditionFilterControl(TableContext tableContext) {
        
        ConditionFilterBoxItem conditionFilterBoxItem = new ConditionFilterBoxItem(tableContext);
        conditionFilterBoxItemList.add(conditionFilterBoxItem);
        
        if (conditionCount <= 0) {
            hBox.getChildren().addAll(conditionFilterBoxItem.getNodeList());
        } else {
            hBox.getChildren().addAll(conditionCount * 3, conditionFilterBoxItem.getNodeList());
        }
        
        conditionCount ++;
        
    }
    
    private ObservableList<Map<String,Object>> buildTableData(String tableName, String whereConditon) {
        
        List<Map<String,Object>> dataList = new TableServiceImpl().getTableData(tableName, whereConditon, null, null);
        ObservableList<Map<String,Object>> tableData = FXCollections.observableArrayList(dataList);
        return tableData;
    }
    
    public HBox gethBox() {
        return hBox;
    }

    public void sethBox(HBox hBox) {
        this.hBox = hBox;
    }
    
}
